package cn.xlbweb.sso.filter;

import cn.xlbweb.sso.constant.SsoProperties;
import cn.xlbweb.sso.entity.User;
import cn.xlbweb.sso.exception.SsoException;
import cn.xlbweb.sso.util.UrlUtils;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.client.RestTemplate;

import javax.servlet.*;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;

/**
 * @author: bobi
 * @date: 2019-03-10 12:01
 * @description:
 */
@Slf4j
public class SsoFilter implements Filter {

    private final String VERIFY_TOKEN_RESULT = "true";

    @Autowired
    private SsoProperties ssoProperties;

    @Autowired
    private RestTemplate restTemplate;

    @Override
    public void init(FilterConfig filterConfig) throws ServletException {
        log.info("SsoFilter init...");
        if (ssoProperties.getProjectId() == null) {
            throw new SsoException("sso project id is null");
        }

        if (ssoProperties.getServerUrl() == null) {
            throw new SsoException("sso server url is null");
        }
    }

    @Override
    public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
        HttpServletRequest request = (HttpServletRequest) servletRequest;
        HttpServletResponse response = (HttpServletResponse) servletResponse;
        HttpSession session = request.getSession();

        // 需要放行的url地址
        String[] excludeUris = ssoProperties.getExcludeUris();
        if (excludeUris != null && excludeUris.length > 0) {
            for (String excludeUri : excludeUris) {
                if (StringUtils.equals(excludeUri, request.getRequestURI())) {
                    log.info("uri={}放行", excludeUri);
                    filterChain.doFilter(request, response);
                    return;
                }
            }
        }

        // 校验是否含有局部session
        User user = (User) session.getAttribute("currentUser");
        if (user != null) {
            filterChain.doFilter(request, response);
            return;
        }

        // 若请求链接中存在token参数
        String token = request.getParameter("token");
        if (StringUtils.isNotBlank(token)) {
            String verifyTokenUrl = UrlUtils.getVerifyTokenUrl(request, session, ssoProperties, token);
            User resultUser = restTemplate.getForObject(verifyTokenUrl, User.class);
            if (resultUser != null) {
                session.setAttribute("currentUser", resultUser);
                filterChain.doFilter(request, response);
                return;
            }
        }

        log.info("正在跳转至sso server认证");
        response.sendRedirect(ssoProperties.getServerUrl() + "/login?redirectUrl=" + UrlUtils.getFullUrl(request));
    }

    @Override
    public void destroy() {
        log.info("SsoFilter destroy...");
    }
}
